package com.liu.community.server.impl;/*
 *@Author:Liu Xing-zhi
 *Description:
 *Date:Crated in 15:28 2021/11/12
 */

import com.liu.community.dto.BlogDto;
import com.liu.community.dto.PageDto;
import com.liu.community.dto.QuestionDto;
import com.liu.community.exception.CustomerException;
import com.liu.community.exception.ErrorCode;
import com.liu.community.mapper.BlogMapper;
import com.liu.community.mapper.UserMapper;
import com.liu.community.pojor.Blog;
import com.liu.community.pojor.Question;
import com.liu.community.pojor.User;
import com.liu.community.server.BlogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private UserMapper userMapper;

    //新创建或者更新Blog博客
    @Override
    public void createOrUpdateBlog(Blog blog) {
        //根据博客id看是否新增或修改，blogID不为空，即数据库中有数据，修改即可，否之新增
        if (blog.getId() == null){
            //新增
            blog.setGmtCreate(System.currentTimeMillis());
            blog.setGmtModified(blog.getGmtCreate());
            blogMapper.createBlog(blog);
        }else {
            //修改
            blog.setGmtModified(System.currentTimeMillis());
            Integer updateBlog = blogMapper.updateBlog(blog);
            if (updateBlog != 1){
                //没有修改成功
                throw new CustomerException(ErrorCode.NOT_UPDATE_SUCCESS);
            }
        }
    }

    @Override
    public BlogDto queryBlogById(Integer id) {
        Blog blog = blogMapper.queryBlogById(id);
        if(blog == null){
            throw  new CustomerException(ErrorCode.BLOG_NOT_FOUND);
        }
        //实例化BlogDto
        BlogDto blogDto = new BlogDto();
        //将blog对象转化为blogDto，并根据blog 获取userId查询user对象，set进blogDto对象中
        BeanUtils.copyProperties(blog,blogDto);
        User user = userMapper.queryUserById(blog.getWriter());
        blogDto.setUser(user);
        return blogDto;
    }

    @Override
    public PageDto<BlogDto> list(Integer id, Integer currentPage, Integer pageSize) {

        PageDto<BlogDto> pageDto = new PageDto<>();
        List<BlogDto> blogDtoList = new ArrayList<>();
        //求总记录数
        Integer dataTotalCount = blogMapper.queryDataTotalCountByUserId(id);
        //求begin边界
        Integer begin =  (currentPage - 1) * pageSize;
        List<Blog> blogList = blogMapper.queryAllBlogById(id,begin, pageSize);
        //遍历集合
        for (Blog blog : blogList) {
            User user = userMapper.queryUserById(blog.getWriter());
            BlogDto blogDto = new BlogDto();
            BeanUtils.copyProperties(blog,blogDto);
            blogDto.setUser(user);
            blogDtoList.add(blogDto);
        }
        pageDto.setData(blogDtoList);
        pageDto.setPageMessage(currentPage,pageSize,dataTotalCount,pageDto);
        return pageDto;
    }

    @Override
    public List<BlogDto> queryRelated(BlogDto queryDto) {
        if (StringUtils.isBlank(queryDto.getTag())){
            return new ArrayList<>();
        }
        String[] split = StringUtils.split(queryDto.getTag(), ",");//将tags分割开
        String collect = Arrays.stream(split).collect(Collectors.joining("|"));
        Blog blog = new Blog();
        blog.setId(queryDto.getId());
        blog.setTag(queryDto.getTag());
        List<Blog> blogs = blogMapper.queryRelated(blog);
        //将blog转换为BlogDto
        List<BlogDto> blogDtoList = blogs.stream().map(newBlog -> {
            BlogDto blogDto = new BlogDto();
            BeanUtils.copyProperties(newBlog, blogDto);
            return blogDto;
        }).collect(Collectors.toList());
        return blogDtoList;
    }


    @Override
    public void increViewCount(Integer id) {
        Blog blog = blogMapper.queryBlogById(id);
        Blog updateBlog = new Blog();
        updateBlog.setViewCount(1);
        updateBlog.setId(id);
        blogMapper.updateViewCount(blog);
    }
}
